package com.cl.unipay4j.demo.example;

/**
 * @Author: kfc
 * @Description: <br/>
 * Date:Create in 2019/7/24 10:33
 * @Modified By:
 */
public class ResultUtil {

    /**
     * 默认成功请求返回信息
     *
     * @return
     */

    public static R success() {
        return result(ResultCodeEnum.SUCCESS);
    }

    /**
     * 自定义返回信息
     *
     * @param resultCodeEnum
     * @return
     */
    public static R success(ResultCodeEnum resultCodeEnum) {
        return new R(ResultCodeEnum.SUCCESS.getCode(), resultCodeEnum.getCode(),resultCodeEnum.getMsg());
    }






    /**
     * 自定义返回信息
     *
     * @param msg
     * @return
     */
    public static R success(String msg) {
        return new R(ResultCodeEnum.SUCCESS.getCode(), msg);
    }


    /**
     * 默认带业务返回参数
     *
     * @param data
     * @return
     */
    public static <T> R<T> success(T data) {
        return success().setData(data);
    }

    /**
     * 默认带业务返回参数
     *
     * @param data
     * @return
     */
    public static <T> R<T> successData(T data) {
        return success().setData(data);
    }

    public static <T> R<T> success(ResultCodeEnum resultCodeEnum, T data){
        return success(resultCodeEnum).setData(data);
    }

    public static <T> R<T> success(String msg, T data){
        return success(msg).setData(data);
    }


    public static R error(){
        return result(ResultCodeEnum.ERROR);
    }

    public static R error(ResultCodeEnum resultCodeEnum){
        return new R(ResultCodeEnum.ERROR.getCode(),resultCodeEnum.getCode(),resultCodeEnum.getMsg());
    }

    public static R error(String msg){
        return new R(ResultCodeEnum.ERROR.getCode(),msg);
    }

    public static R error(int code, String msg){
        return new R(code,msg);
    }


    /**
     * 用户未认证
     *
     * @return
     */

    public static R unauthorized() {
        return result(ResultCodeEnum.UNAUTHORIZED);
    }


    /**
     * 用户认证失败
     *
     * @return
     */

    public static R authorizedFail() {
        return result(ResultCodeEnum.AUTHORIZED_FAIL);
    }


    /**
     * 用户认证失败
     *
     * @return
     */

    public static R authorizedFail(String msg) {
        return result(ResultCodeEnum.SUCCESS,ResultCodeEnum.AUTHORIZED_FAIL,msg);
    }


    public static R unauthorized(ResultCodeEnum resultCodeEnum) {
        return new R(ResultCodeEnum.UNAUTHORIZED.getCode(),resultCodeEnum.getCode(),resultCodeEnum.getMsg());
    }

    public static R unauthorized(String msg) {
        return new R(ResultCodeEnum.UNAUTHORIZED.getCode(),msg);
    }


    /**
     * 访问拒绝
     *
     * @return
     */

    public static R accessDenied() {
        return result(ResultCodeEnum.ACCESS_DENIED);
    }


    /**
     * 404 not found
     *
     * @return
     */
    public static R notFound() {
        return result(ResultCodeEnum.NOT_FOUND);
    }


    public static R accessDenied(ResultCodeEnum resultCodeEnum) {
        return new R(ResultCodeEnum.ACCESS_DENIED.getCode(),resultCodeEnum.getCode(),resultCodeEnum.getMsg());
    }

    /**
     * 系统异常
     *
     * @return
     */
    public static R exception() {
        return result(ResultCodeEnum.EXCEPTION);
    }

    /**
     * 服务熔断
     *
     * @return
     */
    public static R fallBack() {
        return result(ResultCodeEnum.GATEWAY_TIMEOUT);
    }


    private static R result(ResultCodeEnum resultCodeEnum){
        return new R(resultCodeEnum.getCode(),resultCodeEnum.getMsg());
    }


    private static R result(ResultCodeEnum resultCodeEnum, String msg){
        return new R(resultCodeEnum.getCode(),msg);
    }

    private static R result(ResultCodeEnum mainCode, ResultCodeEnum subCode, String msg){
        return new R(mainCode.getCode(),subCode.getCode(),msg);
    }
}
